package com.shangguigu.service.impl;
import com.shangguigu.properties.MinioProperties;
import com.shangguigu.service.FileUploadService;
import io.minio.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioProperties minioProperties;// 修正类名



    @Override
    public String uploadFile(MultipartFile file) {
        try {
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(minioProperties.getEndpoint())
                    .credentials(minioProperties.getAccessKey(),minioProperties.getSecretKey())
                    .build();

            // 检查 bucket 是否存在
            boolean isExist = minioClient.bucketExists(
                    BucketExistsArgs.builder()
                            .bucket(minioProperties.getBucketName())  // 直接使用配置的属性
                            .build()
            );

            if (isExist) {
                log.info("Bucket {} exists", minioProperties.getBucketName());
            } else {
                // 创建 bucket
                minioClient.makeBucket(
                        MakeBucketArgs.builder()
                                .bucket(minioProperties.getBucketName())
                                .build()
                );
                log.info("Bucket {} created", minioProperties.getBucketName());
            }
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String s = UUID.randomUUID().toString().replaceAll("-", "");
            String s1 = substring + s;
            InputStream inputStream = file.getInputStream();//获取输入流
            long size = file.getSize();//获取文件大小

            // 修正 PutObjectArgs 构建
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(minioProperties.getBucketName())  // 正确的 builder 方法
                    .object(s1)
                    .stream(inputStream, size, -1)
                    .contentType(file.getContentType())  // 建议添加内容类型
                    .build();  // 修正拼写

            // 上传文件
            minioClient.putObject(putObjectArgs);

            // 构建访问 URL
            return
                    minioProperties.getEndpoint()+"/"+
                    minioProperties.getBucketName()+"/"+
                    s1;

        } catch (Exception e) {
            log.error("文件上传失败: ", e);
            return null;
        }


    }
    public static void main(){
        String fileName="kun.jpg";
        int i = fileName.lastIndexOf(".");
        System.out.println(i);

        String substring = fileName.substring(i);
        System.out.println(substring);
        String string = UUID.randomUUID().toString();
        System.out.println(string);
    }
}